package com.xh.bgc.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.xh.bgc.pojo.BugInfo;
import com.xh.bgc.pojo.UserInfo;
import com.xh.bgc.services.bug.BugInfoServices;
import com.xh.bgc.util.Mycons;

@RequestMapping(value = "/bugInfo")
@Controller
public class BugController {
	@Autowired
	private BugInfoServices bugInfoBiz;

	/**
	 * 
	 * @param bug
	 * @param session
	 * @return 添加BUG
	 */
	@RequestMapping(value = "/addBug")
	public String addBugInfo(BugInfo bug, HttpSession session) {
		UserInfo userInfo = (UserInfo) session.getAttribute(Mycons.User_Info);
		bug.setAddpeopleid(userInfo.getUserid());
		bugInfoBiz.addBugInfo(bug);
		return "userInfo";
	}

	/**
	 * 
	 * @return
	 */

	@ResponseBody
	@RequestMapping(value = "/ajaxFind", produces = "text/html;charset=UTF-8")
	public String ajaxFind(@RequestParam String description) {

		List<Map<String, Object>> list = bugInfoBiz
				.searchSomething(description);
		return JSON.toJSONString(list);
	}

	/**
	 * 
	 * @param description
	 * @param map
	 * @return json查询
	 */
	@RequestMapping(value = "/searchBug")
	public String findByName(@RequestParam String description,
			Map<String, Object> map) {
		try {
			description = new String(description.getBytes("ISO-8859-1"),
					"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<Map<String, Object>> list = bugInfoBiz.findByName(description);
		map.put("list", list);
		return "bugInfo";
	}

	/**
	 * 
	 * @param description
	 * @param map
	 * @return
	 * 
	 *         按钮查询
	 */
	@RequestMapping(value = "/buttonSearch")
	public String buttonSearch(@RequestParam String description,
			@RequestParam(defaultValue = "1") int indexPage,
			Map<String, Object> map) {
		try {
			description = new String(description.getBytes("ISO-8859-1"),
					"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (description != null && !"".equals(description)) {
			int pageSize = 1;// 每页信息条数
			int tatol = bugInfoBiz.findAllPage(description);// 总条数
			int pageNumber = tatol % pageSize == 0 ? tatol / pageSize : tatol
					/ pageSize + 1;// 总页数

			List<Map<String, Object>> list = bugInfoBiz.buttonSearch(
					description, indexPage, pageSize);
			map.put("list", list);
			map.put("indexPage", indexPage);
			map.put("pageSize", pageSize);
			map.put("pageNumber", pageNumber);
			map.put("description", description);
			return "bugInfo";
		} else {
			return "index";
		}

	}

	/**
	 * 
	 * @param userid
	 * @param description
	 * @return 精确查找
	 */
	@RequestMapping(value = "/searchById")
	public String searchById(@RequestParam(defaultValue = "0") int userid,@RequestParam int queriesNumber,
			@RequestParam int bugid, @RequestParam int status) {
		Map<String, Object> searchById = bugInfoBiz.searchById(bugid);
		System.out.println(bugid);
		int i = bugInfoBiz.insertBugInfo(userid, bugid, status);
		int j = bugInfoBiz.updateNumber(queriesNumber,bugid);
		
		return "index";
	}
}
